from django.shortcuts import redirect, get_object_or_404
from my_friends.models import RSMUser
from my_groups.models import RSMGroup
import string

from django.conf import settings

def getRSMUser(username):
    return get_object_or_404(RSMUser, user__username=username) 

def getRSMGroup(username, groupname):
    return get_object_or_404(RSMGroup, owner=username,groupName=groupname ) 

def getRSMGroup1(groupname):
    return get_object_or_404(RSMGroup,groupName=groupname ) 

def getUserNames(rsmUsers):
    return [u.user.username for u in rsmUsers]

def printDebug(str):
    if settings.DEBUG_PRINTS:
        print str
        
def verifySessionValidity(request):
    if not request.user.is_authenticated():
        return redirect('/?logout=true&reason=expired')
    return None

def getExtend(request):
    return "basic/homepage.html" if request.user.is_authenticated() else "basic/loggedOutMenu.html"

def getTitle(m):
    return m.title

#These functions return True iff s is a string that we consider legal:
def isLegalName(s):
    return __isAcceptableName(s, '"<>&')

def isSafeName(s):
    return __isAcceptableName(s, '"\'<>&')

def __isAcceptableName(s, illegal_chars):
    for c in s:
        if c not in string.printable or c in illegal_chars:
            return False
    return True
